﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Wimm.Common.Entities;
using Wimm.Common.Interfaces.Repositories;
using Wimm.Common.Interfaces.Services;


namespace Wimm.BusinessLogic.Services
{
    public class LoginService :ILoginService
    {
        private IUserRepository _userRepository;

        public LoginService(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public async Task<String> GetAuthenticationResult()
        {
            IReadOnlyCollection<User>  users = await _userRepository.LoadUsers();
            String loginPage = null;
           
            if (users.Count == 0 || users == null)
            {
                loginPage = "registration";
            }
            
            if (users.Count >= 1)
            {
                loginPage = "multi";
            }
            return loginPage;
        }

        public async Task<bool> CheckUserPassword(string user, string password)
        {
            User u = user == null ?
                await _userRepository.LoadUser() :
                await _userRepository.FindUserByLogin(user);

            if (u == null)
                return false;

            return String.Compare(u.Password, password) == 0;
        }

        public async Task<bool> UserLogin(string user)
        {
            User u = await _userRepository.FindUserByLogin(user);

            if (u == null)
            {
                return true;
            }

            return false;
        }

        public async Task<User> UserPassword(string password)
        {
            User u = await _userRepository.FindUserByPassword(password);

            //if (u == null)
            //{
            //    return false;
            //}
            //return true;
            return u;
        }

        public async Task<User> UserLoginAndPassword(string login, string password)
        {
            User u = await _userRepository.FindUserByLoginAndPassword(login, password);

            return u;
        }
    }
}
